package cn.com.nes.site.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 执行器异步配置
 */
@Configuration
public class ExecutorAsyncConfig {

    @Bean(name = "asyncExecutor")
    public Executor newAsync() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(10);   // 核心线程数
        taskExecutor.setMaxPoolSize(100);   // 最大线程数，只有在缓存队列满了以后才会申请超过核心线程数的线程
        taskExecutor.setQueueCapacity(50);  // 缓存队列
        taskExecutor.setKeepAliveSeconds(200); //线程空闲存活时间
        taskExecutor.setThreadNamePrefix("my-AsyncExecutor-"); //异步方法内部线程名称前缀
        taskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //拒绝策略
        taskExecutor.initialize();
        return taskExecutor;
    }
}
